package com.starbaba.mainicon.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.mainicon.model.HomeBannerModel;
import com.starbaba.mainicon.model.HomeBannerVO;
import com.starbaba.mainicon.service.HomeBannerService;

@Component(value="homeBannerServiceImpl")
@SuppressWarnings({"rawtypes","unchecked"})
public class HomeBannerServiceImpl  extends CommonServiceSupport implements HomeBannerService {
	
	private static final long serialVersionUID = -5108922671733677590L;
	
    public List<HomeBannerVO> getDataList(int iDisplayStart, int iDisplayLength,
			HomeBannerModel model, PageResponse pageResponse , CommonService server) {
		StringBuffer sf = new StringBuffer();
		sf.append(" from HomeBannerVO a where 1 = 1 ");
		String condition = bulidCondition(model, server);
		sf.append(condition);
		sf.append(" order by a.orderby asc ");
		String hql = sf.toString();
		String countHql = "select count(*) " + hql;
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		Query query = dao.getSession().createQuery(hql);
		
		if(iDisplayLength != 0)
		{
			query.setFirstResult(iDisplayStart);
			query.setMaxResults(iDisplayLength);
		}
		List<HomeBannerVO> list = query.list();
		List<HomeBannerVO> returnList = new ArrayList<HomeBannerVO>();
		HomeBannerVO itemVo;
		@SuppressWarnings("unused")
        Map<Long ,HomeBannerVO> map = new HashMap<Long ,HomeBannerVO>();
		
		for(HomeBannerVO vo :list)
		{
			itemVo = new HomeBannerVO();
		
			itemVo.setId(vo.getId());
			itemVo.setChannel(vo.getChannel());
			itemVo.setEnd_time(vo.getEnd_time());
			itemVo.setImgurl(vo.getImgurl());
			itemVo.setPkgname(vo.getPkgname());
			itemVo.setLaunch_params(vo.getLaunch_params());
			itemVo.setPlatform(vo.getPlatform());
			itemVo.setStatus(vo.getStatus());
			itemVo.setType(vo.getType());
			itemVo.setContent(vo.getContent());
			returnList.add(itemVo);
		}
		
		Query countQuery = dao.getSession().createQuery(countHql);
		Object obj = countQuery.uniqueResult();
		if(obj instanceof Long)
		{
			int count = ((Long)obj).intValue();
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return returnList;
	}

	private String bulidCondition(HomeBannerModel model, CommonService server) {
		StringBuffer condition = new StringBuffer();

		String channel = model.getChannel();
		if(!StringUtils.isEmpty(channel)) {
			condition.append(" and a.channel = " + channel);
		}
		
		Integer platform = model.getPlatform();
		if(null != platform) {
			condition.append(" and a.platform = " + platform);
		}
		
		Integer status = model.getStatus();
		if(null != status) {
			condition.append(" and a.status = " + status);
		}
		
		Integer type = model.getType();
		if(null != type) {
			condition.append(" and a.type = " + type);
		}
		
		return condition.toString();
	}

}
